package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.exception.RootException;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.ContainerDetailDao;
import com.cxyd.modules.wms.dto.ContainerDetailDTO;
import com.cxyd.modules.wms.entity.ContainerDetailEntity;
import com.cxyd.modules.wms.service.ContainerDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * 货格信息
 *
 * @author lp
 * @since 1.0.0 2024-08-18
 */
@Service
public class ContainerDetailServiceImpl extends CrudServiceImpl<ContainerDetailDao, ContainerDetailEntity, ContainerDetailDTO> implements ContainerDetailService {
    private final SysUserService userService;
    private final ContainerDetailDao containerDetailDao;

    public ContainerDetailServiceImpl(SysUserService userService, ContainerDetailDao containerDetailDao) {
        this.userService = userService;
        this.containerDetailDao = containerDetailDao;
    }

    @Override
    public PageData<ContainerDetailDTO> page(Map<String, Object> params) {
        //1.查询容器信息
        final IPage<ContainerDetailEntity> page = baseDao.selectPage(
                getPage(params, "cont_detail_id", true),
                getWrapper(params)
        );
        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return getPageData(page, ContainerDetailDTO.class);
    }

    @Override
    public QueryWrapper<ContainerDetailEntity> getWrapper(Map<String, Object> params) {
        String contNo = (String) params.get("contNo");

        QueryWrapper<ContainerDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);

        return wrapper;
    }
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void delete(@RequestBody Long[] ids) {
//        for (int i = 0; i < ids.length; i++) {
//            deleteContById(ids[i]);
//        }
//    }
//    public void deleteContById(Long id) {
//        ContainerEntity entity = containerDetailDao.getContById(id);
//        if (entity != null) {
//            throw new RootException("托盘已存在于库存中，不能删除！");
//        }
//        baseDao.deleteById(id);
//    }

}